﻿using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using PortfolioFinalProject.DataModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace PortfolioFinalProject.Data
{
    public class Seeder
    {
        public static void Seed(ApplicationDbContext db, bool seedUsers = true, bool seedSkills = true, bool seedProjects = true)
        {
            List<ApplicationUser> UserList = new List<ApplicationUser>();

            var admin = new ApplicationUser()
            {
                UserName = "HTC",
                Email = "taaha.inc@gmail.com",
            };
            UserList.Add(admin);

            if (seedUsers)
            {
                var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
                roleManager.Create(new IdentityRole { Name = "Admin" });
                UserManager<ApplicationUser> um = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

                foreach (var user in UserList)
                {
                    if (um.FindByName(user.UserName) == null)
                    {
                        var result = um.Create(user, "pass01");
                    }
                }
                db.SaveChanges();
                var au = db.Users.Where(x => x.UserName == "HTC").FirstOrDefault();
                um.AddToRole(au.Id, "Admin");
            }

            if (seedSkills)
            {
                db.Skills.AddOrUpdate(
                    s => s.Title,
                    new Skill("http://i.imgur.com/YPSduxy.png", "C#", "C# land is definetly where I love to be."),
                    new Skill("http://i.imgur.com/7QNL31F.png", "HTML 5", "Though I do not prefer designing, I am pretty proficient in building a basic structure using HTML.")
                    );
                db.SaveChanges();
            }

            if (seedProjects)
            {
                db.Projects.AddOrUpdate(
                    p => p.Title,
                    new Project("Game Oasis", "Game Oasis is site where gamers can come and upload their library and rent, buy, or trade games with peers.", "HTML/CSS, JavaScript/AngularJS, C#/ASP.NET MVC Web API, SQL", "https://projectgamer.codeplex.com/", "http://gameoasis.azurewebsites.net/#/", "http://i.imgur.com/IHBRHBe.png",".Net")
                    );
                db.SaveChanges();
            }
        }
    }
}
